package cn.comon;

import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class RedisUtil implements AutoCloseable {
    private Jedis jedis;

    public RedisUtil() {
        Properties prop = new Properties();
        try {
            prop.load(Files.newInputStream(Paths.get("config/login.properties")));
            String host = prop.getProperty("host");
            int port = Integer.parseInt(prop.getProperty("port"));

            jedis = new Jedis(host, port);
            System.out.println("连接Redis成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String get(String key) {
        return jedis.get(key);
    }

    public void set(String key, String value) {
        jedis.set(key, value);
    }

    public void delete(String key) {
        jedis.del(key);
    }

    @Override
    public void close() {
        if (jedis != null) {
            jedis.close();
        }
    }
}
